
import { $sunDelId } from '@/sun-components/SunArray.js'
/**
 * 构建formData
 */
export function ctFormData(data) {
  const returnData = {}
  data?.forEach((item) => {
    if (item.category === 'field') {
      returnData[item.key] = (item.type === 'file' || item.type === 'files') ? [] : ''
    }
    if (item.category === 'group') {
      returnData[item.key] = {}
      item.children?.forEach((el) => {
        returnData[item.key][el.key] = (el.type === 'file' || el.type === 'files') ? [] : ''
      })
    }
  })
  console.log('构建formData-------------', returnData)
  return returnData
}

/**
 * 初始化formData
 */
export function initFormData(from, data) {
  const formKeyList = Object.keys(from)
  data.forEach((item) => {
    if (!formKeyList.includes(item.key)) return
    if (item.category === 'field') {
      from[item.key] = item.content ? item.content : (item.type === 'file' || item.type === 'files') ? [] : ''
    }
    if (item.category === 'group') {
      const elFormKeyList = Object.keys(from[item.key])
      // from[item.key] = {}
      from[item.key] = elFormKeyList.length ? from[item.key] : {}
      item.children?.forEach((el) => {
        if (!elFormKeyList.includes(el.key)) return
        from[item.key][el.key] = el.content ? el.content : (el.type === 'file' || el.type === 'files') ? [] : ''
      })
    }
  })
  console.log('初始化formData', from)
  return from
}

/**
 * formData填充
 */
export function submitInformation(from, data, source) {
  const returnData = []
  const formKeyList = Object.keys(from)
  data.forEach((info) => {
    // 检查分组是否存在老师字段和课程中心字段 注数据返回后必须外层去重子元素叠加
    if (source === 'oldData' && formKeyList.includes(info.key) && info.category === 'group') {
      const elFormKeyList = Object.keys(from[info.key])
      const childrenContent = []
      info.children?.forEach((infochildren) => {
        // 在from中children中不存在即为课程中心添加字段
        if (!elFormKeyList.includes(infochildren.key)) {
          childrenContent.push({
            'key': infochildren.key,
            'name': infochildren.name,
            'category': infochildren.category,
            'type': infochildren.type,
            'displayorder': infochildren.displayorder,
            'content': infochildren.content
          })
          returnData.push({
            'key': info.key,
            'name': info.name,
            'category': info.category,
            'displayorder': info.displayorder,
            'children': childrenContent
          })
        }
      })
    }
    // 获取课程中心分组或者字段
    if (source === 'oldData' && !formKeyList.includes(info.key)) {
      if (info.category === 'field') {
        returnData.push({
          'key': info.key,
          'name': info.name,
          'category': info.category,
          'type': info.type,
          'displayorder': info.displayorder,
          'content': info.content
        })
      }
      if (info.category === 'group') {
        const childrenContent = []
        info.children?.forEach((infochildren) => {
          childrenContent.push({
            'key': infochildren.key,
            'name': infochildren.name,
            'category': infochildren.category,
            'type': infochildren.type,
            'displayorder': infochildren.displayorder,
            'content': infochildren.content
          })
        })
        returnData.push({
          'key': info.key,
          'name': info.name,
          'category': info.category,
          'displayorder': info.displayorder,
          'children': childrenContent
        })
      }
    }
    // 模板中的字段信息
    if (source === 'newData' && formKeyList.includes(info.key)) {
      if (info.category === 'field') {
        returnData.push({
          'key': info.key,
          'name': info.name,
          'category': info.category,
          'type': info.type,
          'displayorder': info.displayorder,
          'content': from[info.key]
        })
      }
      if (info.category === 'group') {
        const childrenContent = []
        info.children?.forEach((infochildren) => {
          childrenContent.push({
            'key': infochildren.key,
            'name': infochildren.name,
            'category': infochildren.category,
            'type': infochildren.type,
            'displayorder': infochildren.displayorder,
            'content': from[info.key][infochildren.key]
          })
        })
        returnData.push({
          'key': info.key,
          'name': info.name,
          'category': info.category,
          'displayorder': info.displayorder,
          'children': childrenContent
        })
      }
    }
  })
  console.log('需要提交的数据-----------', returnData)
  return $sunDelId(returnData, 'key')
}

/**
 * 数组key去重合并children
 */
export function unique(data) {
  const dataKeyList = []
  const dataList = []
  data.forEach(item => {
    if (dataKeyList.includes(item.key)) {
      const index = dataKeyList.findIndex(fruit => fruit === item.key)
      dataList[index].children = [...dataList[index].children, ...item.children]
    } else {
      dataList.push(item)
      dataKeyList.push(item.key)
    }
  })
  return dataList
}

/**
 * 选择文件后缀处理
 */
export function setAccept(exf) {
  let returnData = ''
  let exf_arry = []
  const exf_keys = Object.keys(exf || [])
  exf_keys?.forEach((e) => {
    exf_arry = [...exf_arry, ...exf[e]]
  })
  returnData = exf_arry.join(',').replace(/\s/g, '')
  return returnData
}

/**
 * 资料驳回时获取意见
 */
export function getAuditComment(data) {
  const key_comment = {}
  data?.forEach((item) => {
    if (item.category === 'field') {
      key_comment[item.key] = item.audit_comment || ''
    }
    if (item.category === 'group') {
      key_comment[item.key] = {}
      item.children?.forEach((el) => {
        key_comment[item.key][el.key] = el.audit_comment || ''
      })
    }
  })
  console.log('获取审核意见------', key_comment)
  return key_comment
}
